{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Executing Multiple Workflows (Lattices)\n",
    "\n",
    "Execute multiple lattices from the same script.\n",
    "\n",
    "In Covalent there is no reason you cannot dispatch multiple lattices from the same script. Each dispatch is tracked separately and generates a different result set.\n",
    "\n",
    "You might run two lattices from the same script if, for instance, the lattices represent two different approaches to the same problem and you want to programmatically compare the result sets. \n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "[Start the Covalent services](./covalent_cli).\n",
    "\n",
    "### Procedure\n",
    "\n",
    "1. Write the lattices: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "INF_LIMIT = 50\n",
    "\n",
    "# Calculate e based on a formula\n",
    "@ct.electron\n",
    "def e_exp(x):\n",
    "    return (1 + 1/x) ** x\n",
    "\n",
    "# Calculate e based on a series\n",
    "@ct.electron    \n",
    "def e_ser(x):\n",
    "    e_est = 1\n",
    "    fact = 1\n",
    "    for i in range(1, x):\n",
    "        fact *= i\n",
    "        e_est += 1/fact\n",
    "    return e_est\n",
    "\n",
    "@ct.lattice\n",
    "def wf_exp(x):\n",
    "    return e_exp(x)\n",
    "\n",
    "@ct.lattice\n",
    "def wf_ser(x):\n",
    "    return e_ser(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Dispatch the lattices separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "wf_exp_id = ct.dispatch(wf_exp)(INF_LIMIT)\n",
    "wf_ser_id = ct.dispatch(wf_ser)(INF_LIMIT)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Compare the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.691588029073608\n",
      "2.7182818284590455\n"
     ]
    }
   ],
   "source": [
    "r_exp = ct.get_result(wf_exp_id, wait=True)\n",
    "r_ser = ct.get_result(wf_ser_id, wait=True)\n",
    "\n",
    "print(r_exp.result)\n",
    "print(r_ser.result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Looping](../coding/looping.ipynb)\n",
    "\n",
    "[Executing a Lattice Multiple Times](./execute_lattice_multiple_times.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
